let app = getApp();
let audioInfo = app.globalData.audioInfo;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    kitapCon: '', // 书籍信息
    audioList: [],
    playAllMask: false,
    isDetailed: false,
    isLoading: true, // 是否加载完成
    isListLoading: true, // 是否列表加载完成
    notificationShow: false,
    notificationMessage: 'سىستىمغا كىردىڭىز !',
    playId: 0, // 当前播放音频
    pageNo: 1,
    pageSize: 100,
    totalPage: 0,
    totalCount: 0,
    isLoginMask: false, 
  },

  // 获取书籍信息
  getBookInfo(postId, isLoading) {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) return this.setData({ isLoginMask: true }); // 不登陆不往下执行

    let bookId = postId;
    let that = this;
    wx.request({
      url: `${app.globalData.API_URL}/kitapCon/${bookId}`,
      header: app.header(),
      success(res) {
        if (res.statusCode) {
          that.setData({ kitapCon: res.data });
          if(isLoading) return that.LoadingFinish(); // 完成加载
        }
      },
    });
  },

  // 获取集
  getAudio() {
    let bookId = this.data.postId;
    let pageNo = this.data.pageNo;
    let pageSize = this.data.pageSize;
    let that = this;
    wx.request({
      url: 'https://api-kitap.qiyat.cn/getAudio',
      method: 'GET',
      data: { bookId, pageNo, pageSize },
      success(res) {
        app.globalData.audioList = res.data.data;
        that.setData({
          audioList: res.data.data,
          totalPage : res.data.totalPage, totalCount: res.data.totalCount
        });
        that.ListLoadingFinish();
      },
    });
  },

  // 点击集播放
  onChapter(item) {
    // 检查是否免费或者是否购买会员
    let isFree = item.currentTarget.dataset.item.is_free;
    if(isFree==1 && !app.globalData.isVIPAuthority) return this.setData({playAllMask: true})
    
    app.globalData.chapterName = item.currentTarget.dataset.item.chapter_name; // 最小画章节名
    app.globalData.isLittleDrawingVisible = true; // 开启播放最小画

    let itemPost = item.currentTarget.dataset.item
    audioInfo.playId = itemPost.id;
    audioInfo.isPlaying = true;
    this.setData({ playId: itemPost.id })

    // 创建后台播放
    audioInfo.bgAudio = wx.getBackgroundAudioManager()
    audioInfo.bgAudio.title = itemPost.name;
    audioInfo.bgAudio.singer = itemPost.chapter_name;
    audioInfo.bgAudio.coverImgUrl = this.data.kitapCon.cover_wx_url;
    audioInfo.bgAudio.src = itemPost.wx_url;

    // 前往播放页面
    let postId = this.data.postId;
    wx.navigateTo({
      url: `/pages/audioPlay/audioPlay?postId=`+postId,
    })
  },

  // 前往购买会员
  onPlay() {
    wx.navigateTo({
      url: `/pages/member/member`,
    })
  },

  // 点击介绍
  onDetailed() {
    this.setData({ isDetailed: true })
  },

  // 点击大集
  onClickLarge(e) {
    let isLoading = false;
    this.setData({ isListLoading: true, isLoading: false })
    let kitab_id = e.currentTarget.dataset.kitab_id;
    this.setData({ postId: kitab_id });
    this.getBookInfo(kitab_id, isLoading);
    this.getAudio();
  },

  // 全部播放
  onPlayAll() {
    // 检查是否免费或者是否购买会员
    let isFree = this.data.kitapCon.is_free;
    if(isFree==1 && !app.globalData.isVIPAuthority) return this.setData({playAllMask: true});

    let dataItem = this.data.audioList;
    let itemPost = dataItem[0]
    audioInfo.playId = itemPost.id;
    audioInfo.isPlaying = true;
    this.setData({ playId: itemPost.id })

    // 创建后台播放
    audioInfo.bgAudio = wx.getBackgroundAudioManager()
    audioInfo.bgAudio.title = itemPost.name;
    audioInfo.bgAudio.singer = itemPost.chapter_name;
    audioInfo.bgAudio.coverImgUrl = this.data.kitapCon.cover_wx_url;
    audioInfo.bgAudio.src = itemPost.wx_url;

    // 前往播放页面
    let postId = this.data.postId;
    wx.navigateTo({
      url: `/pages/audioPlay/audioPlay?postId=`+postId,
    })

  },

  // 检查用户登录
  isLogin() {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      this.setData({ isLoginMask: true })
    }
  },

  // 隐藏登录提示组件
  changeLoginMask(e) {
    this.setData({ isLoginMask: e.detail.isLoginMask, notificationShow: true })
    this.getBookInfo(this.data.postId, true)
  },

  // 集加载完成
  ListLoadingFinish() {
    setTimeout(() => {
      this.setData({ isListLoading: false })
    }, 500)
  },

  // 加载完成
  LoadingFinish() {
    setTimeout(() => {
      this.setData({ isLoading: false, isListLoading: false })
    }, 500)
  },

  // باب بۇيىچە ئېرىشىش
  byBabgetAudio(e) {
    let bolum = e.currentTarget.dataset.bab;
    let that = this;
    wx.request({
      url: `${app.globalData.API_URL}/getBolumAudio`,
      method: 'POST',
      data: { bolum_id: bolum.id, book_id: bolum.book_id },
      success(res) {
        that.setData({ 
          audioList: res.data,
        });
        that.ListLoadingFinish();
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let isLoading = true;
    this.setData({ postId: options.postId, playId: audioInfo.playId });
    this.getBookInfo(options.postId, isLoading);
    this.getAudio();
    this.isLogin();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({ playId: audioInfo.playId });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: `${this.data.kitapCon.name}`,
      path: `/pages/audioList/audioList?postId=` + this.data.postId,
      imageUrl: `${this.data.kitapCon.cover_wx_url}`
    };
  }
})